package com.lxl.lxlinterface.controller;

import cn.hutool.http.HttpRequest;
import com.lxl.lxlinterface.model.User;
import com.lxl.lxlinterface.utils.SignUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/name")
public class NameController {

    @GetMapping("/")
    public String getNameByGet(@RequestParam String name){

        return "Get 你的名字是"+name;
    }

    @PostMapping ("/")
    public String getNameByPost(@RequestParam String name){

        return "Post 你的名字是"+name;
    }

    @PostMapping("/user")
    public String getUserNameByGet(@RequestBody User user, HttpServletRequest request) {

        //校验
        //ak
        String accessKey = request.getHeader("accessKey");
        // 时间戳
        String timestamp = request.getHeader("timestamp");
        // 随机数
        String nonce = request.getHeader("nonce");
        String sign = request.getHeader("sign");
        // 用户
        String us = request.getHeader("user");

        //todo 检查用户是否有ak
        //检验随机数
        if (Long.parseLong(nonce)>10000){
            throw new RuntimeException("无权限");
        }
        //检验时间戳
        if (Math.abs(Long.parseLong(timestamp)-System.currentTimeMillis()/1000) > 120){
            throw new RuntimeException("签名已过期");
        }
        //检验sign值
        String genSign = SignUtils.genSign(us, "abcdefg");
        // 如果签名不相同
        if(!genSign.equals(sign)){
            throw new RuntimeException("无权限");
        }
        System.out.println(request);
        return "Post 你的名字是" + user.getName();
    }

}
